import LinkListNode from "./LinkList";

export class LinkedListRange<T> {
    get first(): LinkListNode<T> {
        return this._first;
    }

    get terminal(): LinkListNode<T> {
        return this._terminal;
    }
    private readonly _first:LinkListNode<T>;
    private readonly _terminal:LinkListNode<T>;

    constructor(first:LinkListNode<T>,terminal:LinkListNode<T>) {
        if (first == null || terminal == null || first == terminal)
        {
            throw new Error("Range is invalid.");
        }
        this._first = first;
        this._terminal = terminal;
    }

    get IsValid(){
        return this._first != null && this._terminal != null && this._first != this._terminal;
    }

    get Count(){
        if (!this.IsValid)
        {
            return 0;
        }

        let count:number = 0;
        for (let current:LinkListNode<T> = this._first; current != null && current != this._terminal; current = current.Next)
        {
            count++;
        }

        return count;
    }

    Contains(value:T):boolean
    {
        for (let current:LinkListNode<T> = this._first; current != null && current != this._terminal; current = current.Next)
        {
            if (current.Value === value)
            {
                return true;
            }
        }
        return false;
    }



}